using Myotragus.Data.Containers;
using Myotragus.Data.Criteria;
using Myotragus.Data.Domain;

namespace Myotragus.Data.Repositories
{
	public class LocalizableReadRepository<TEntity> :
		LocalizableRepository<TEntity>,
		ILocalizableReadRepository<TEntity>
		where TEntity : IEntity
	{
		public LocalizableReadRepository(IEntitiesContainer entities,
			ILocalizableQueryBuilder<TEntity> queryBuilder)
			: base(entities)
		{
			QueryBuilder = queryBuilder;
		}

		protected ILocalizableQueryBuilder<TEntity> QueryBuilder { get; private set; }

		public virtual IPagedQuery<TEntity> FindAll(int lcId)
		{
			return QueryBuilder.CreateQuery<TEntity>(QueryBuilder.CreateCriteria(lcId));
		}

		public virtual int CountAll(int lcId)
		{
			return QueryBuilder.Count(QueryBuilder.CreateCriteria(lcId));
		}

		public virtual IPagedQuery<TEntity> FindMatching(int lcId, object pattern)
		{
			return QueryBuilder.CreateQuery<TEntity>(QueryBuilder
				.CreateMatchingCriteria(lcId, pattern));
		}

		public virtual int CountMatching(int lcId, object pattern)
		{
			return QueryBuilder.Count(QueryBuilder.CreateMatchingCriteria(lcId, pattern));
		}

		public virtual IPagedQuery<TEntity> FindLike(int lcId, object pattern)
		{
			return QueryBuilder.CreateQuery<TEntity>(QueryBuilder
				.CreateLikeCriteria(lcId, pattern));
		}

		public virtual int CountLike(int lcId, object pattern)
		{
			return QueryBuilder.Count(QueryBuilder.CreateLikeCriteria(lcId, pattern));
		}
	}
}